package com.unlcn.ils.kas.controller.order;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.unlcn.ils.kas.annotation.LogRecord;
import com.unlcn.ils.kas.dao.model.order.Customer;
import com.unlcn.ils.kas.dao.model.order.OrderOtd;
import com.unlcn.ils.kas.dao.model.order.bo.OrderLineBo;
import com.unlcn.ils.kas.dao.model.order.bo.OrderModifyInfoBo;
import com.unlcn.ils.kas.service.order.OrderOtdService;
import com.unlcn.ils.kas.service.util.BaseException;
import com.unlcn.ils.kas.service.util.enums.CommonExceptionEnum;
import com.unlcn.ils.kas.service.util.enums.order.OrderExceptionEnum;
import com.unlcn.ils.kas.vo.RestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单接口
 * @author 罗必量
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderOtdService orderOtdService;

    /**
     * 获取订单列表
     * @param page
     * @return
     */
    @PostMapping(value = "/queryPage")
    public RestVO<OrderLineBo> queryPage(Page<OrderOtd> page) {
        LOGGER.error("OrderController.queryPage（获取订单列表） params : {} ", JSON.toJSONString(page));

        RestVO<OrderLineBo> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.queryPage(page), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.queryPage ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    /**
     * 查找编辑订单信息
     * @param id
     * @return
     */
    @PostMapping(value = "/findModifyInfo")
    public RestVO<OrderModifyInfoBo> findModifyInfo(Long id) {
        LOGGER.error("OrderController.findModifyInfo（获取编辑订单信息） params : {} ", id);

        if (id == null)
            throw new BaseException(OrderExceptionEnum.ID_NOT_NULL.getCode(), OrderExceptionEnum.ID_NOT_NULL.getText());

        RestVO<OrderModifyInfoBo> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.findModifyInfo(id), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.findModifyInfo ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    /**
     * 更新订单信息
     * @param orderModifyInfoBo
     * @return
     */
    @LogRecord(description = "更新订单信息")
    @PostMapping(value = "/modifyInfo")
    public RestVO<OrderLineBo> modifyInfo(@RequestBody OrderModifyInfoBo orderModifyInfoBo) {
        LOGGER.error("OrderController.modifyInfo（保存编辑订单信息） params : {} ", JSON.toJSONString(orderModifyInfoBo));

        if (orderModifyInfoBo == null)
            throw new BaseException(OrderExceptionEnum.MODIFY_INFO_NOT_NULL.getCode(), OrderExceptionEnum.MODIFY_INFO_NOT_NULL.getText());

        RestVO<OrderLineBo> restVO = null;

        try {
            orderOtdService.modifyInfo(orderModifyInfoBo);
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.modifyInfo ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getLatLon")
    public RestVO<OrderLineBo> getLatLon(String address, String endAddress) {
        LOGGER.error("OrderController.getLatLon（获取地址经纬度） params : {} ", address);

        RestVO<OrderLineBo> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getLatLon(address, endAddress), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getLatLon ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getCustomer")
    public RestVO<Customer> getCustomer() {
        LOGGER.error("OrderController.getCustomer（获取客户） params : {} ");

        RestVO<Customer> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getCustomer(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getCustomer ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getTransportType")
    public RestVO<String> getTransportType() {
        LOGGER.error("OrderController.getTransportType（获取运输方式） params : {} ");

        RestVO<String> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getTransportType(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getTransportType ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getSupplier")
    public RestVO<String> getSupplier() {
        LOGGER.error("OrderController.getSupplier（获取无车承运人） params : {} ");

        RestVO<String> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getSupplier(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getSupplier ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getDealerName")
    public RestVO<String> getDealerName() {
        LOGGER.error("OrderController.getDealerName（获取供应商） params : {} ");

        RestVO<String> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getDealerName(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getDealerName ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }

    @GetMapping(value = "/getShipmentType")
    public RestVO<String> getShipmentType() {
        LOGGER.error("OrderController.getShipmentType（获取发运类型） params : {} ");

        RestVO<String> restVO = null;

        try {
            restVO = new RestVO(CommonExceptionEnum.SUCCESS.getCode(), orderOtdService.getShipmentType(), CommonExceptionEnum.SUCCESS.getText());
        } catch (Exception e) {
            LOGGER.error("OrderController.getDealerName ERROR Message : {} ", e);
            restVO = new RestVO<>(CommonExceptionEnum.ERROR.getCode(),CommonExceptionEnum.ERROR.getText());
        }

        return restVO;
    }
}
